package com.glory.wxelectricitypda.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.DefectListDataAdapter;
import com.glory.wxelectricitypda.base.App;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.LotDefectListBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class DefectRecordActivity extends BaseDialogActivity {

    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etStepName)
    FreeUI_ClearEditText etStepName;
    @BindView(R.id.etMainQty)
    FreeUI_ClearEditText etMainQty;
    @BindView(R.id.rvDefectList)
    RecyclerView rvDefectList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flDefectList)
    FrameLayout flDefectList;
    @BindView(R.id.tvDefect)
    TextView tvDefect;
    @BindView(R.id.ivDefect)
    ImageView ivDefect;
    @BindView(R.id.tvAddDefect)
    TextView tvAddDefect;

    private DefectListDataAdapter mDefectListDataAdapter;
    private FreeUI_GeneralPop mPositionPop;
    private ArrayList<String> codeList = new ArrayList<>();
    private ArrayList<String> descList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_defect_record;
    }

    @Override
    protected void initView() {
        rvDefectList.setLayoutManager(new LinearLayoutManager(DefectRecordActivity.this));
        rvDefectList.setItemAnimator(new DefaultItemAnimator());
        rvDefectList.addItemDecoration(new DividerItemDecoration(DefectRecordActivity.this, 1));
        mDefectListDataAdapter = new DefectListDataAdapter(null);
        mDefectListDataAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mDefectListDataAdapter.isFirstOnly(true);
        rvDefectList.setAdapter(mDefectListDataAdapter);

        mDefectListDataAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                FreeApi_DialogUtils.getConfirmAndCancelDialog(DefectRecordActivity.this, "是否确认删除?", new FreeUI_CommonCallback() {
                    @Override
                    public void onSuccess() {
                        JSONObject map = new JSONObject();
                        JSONObject map1 = new JSONObject();
                        try {
                            map1.put("ACTIONTYPE", "DELETE");
                            LotDefectListBean.DefectInfoEntity defectInfoEntity = new LotDefectListBean.DefectInfoEntity();
                            defectInfoEntity.setLOT_ID(mDefectListDataAdapter.getData().get(i).getLOT_ID());
                            defectInfoEntity.setSCRAP_CODE(mDefectListDataAdapter.getData().get(i).getSCRAP_CODE());
                            map1.put("LOTDEFECTIVEINPUT", new JSONObject(new Gson().toJson(defectInfoEntity)));
                            map.put("Data",map1);
                            map.put("MessageName","EUIT.LOTDEFECTIVE");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        NetUtils.requestPostMesForwardUtils(map,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotDefectListBean>>(
                                new TypeToken<ResponseBean<LotDefectListBean>>() {
                                }.getType(), getSupportFragmentManager(), this) {
                            @Override
                            public void onSuccess(Response<ResponseBean<LotDefectListBean>> response) {
                                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                                    showToast("删除成功",true);
                                    mDefectListDataAdapter.remove(i);
                                    mDefectListDataAdapter.notifyDataSetChanged();
                                }
                            }

                            @Override
                            public void onError(Response<ResponseBean<LotDefectListBean>> response) {
                                super.onError(response);
                                loadError(response.getException(), "LOTDEFECTIVE");
                            }
                        });
                    }
                }, () -> {}).show(getSupportFragmentManager(),"DELETE_DIALOG");
            }
        });

    }

    @Override
    protected void initData() {
//        getDelectInfo("");

        initEdit(etLotId, this::getDelectInfo);
        getScrapcodeList();
    }

    private void getScrapcodeList() {
        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "GETBYREFNAME");
            map.put("REFNAME", "EUIT_DEFEC_CODE");
            jsonObject.put("Data",map);
            jsonObject.put("MessageName","EUIT.EuitADUREFLISTHANDLER");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                new TypeToken<ResponseBean<LotBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LotBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    for (LotBean.AdurefBean adurefBean : response.body().getBody().getADUREFLIST()) {
                        codeList.add(adurefBean.getKEYID());
                        descList.add(adurefBean.getTEXT());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<LotBean>> response) {
                super.onError(response);
                loadError(response.getException(), "ADUREFLISTHANDLER");
            }
        });

    }

    private void getDelectInfo(String lotid) {
        JSONObject map = new JSONObject();
        JSONObject map1 = new JSONObject();
        try {
            map1.put("ACTIONTYPE", "QUERY");
            LotDefectListBean.DefectInfoEntity defectInfoEntity = new LotDefectListBean.DefectInfoEntity();
            defectInfoEntity.setLOT_ID(lotid);
            map1.put("LOTDEFECTIVEINPUT", new JSONObject(new Gson().toJson(defectInfoEntity)));
            map.put("Data",map1);
            map.put("MessageName","EUIT.LOTDEFECTIVE");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(map,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotDefectListBean>>(
                new TypeToken<ResponseBean<LotDefectListBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LotDefectListBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    mDefectListDataAdapter.setNewData(response.body().getBody().getLOTDEFECTIVEINPUTLIST());
                }
            }

            @Override
            public void onError(Response<ResponseBean<LotDefectListBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTDEFECTIVE");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("不良临时记录表");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.flDefectList,R.id.tvAddDefect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvAddDefect:
                if (etLotId.getText().toString().equals("")){
                    showToast("批次号不能为空",false);
                    return;
                }
                if (tvDefect.getText().toString().equals("")){
                    showToast("批次号不能为空",false);
                    return;
                }
                JSONObject map = new JSONObject();
                JSONObject map1 = new JSONObject();
                try {
                    map1.put("ACTIONTYPE", "SAVE");
                    LotDefectListBean.DefectInfoEntity defectInfoEntity = new LotDefectListBean.DefectInfoEntity();
                    defectInfoEntity.setLOT_ID(etLotId.getText().toString());
                    defectInfoEntity.setSTEP_NAME(etStepName.getText().toString());
                    defectInfoEntity.setMAIN_QTY(etMainQty.getText().toString());
                    String code = "";
                    for (int i = 0; i < descList.size(); i++) {
                        if (tvDefect.getText().toString().equals(descList.get(i))){
                            code = codeList.get(i);
                        }
                    }
                    defectInfoEntity.setSCRAP_CODE(code);
                    map1.put("LOTDEFECTIVEINPUT", new JSONObject(new Gson().toJson(defectInfoEntity)));
                    map.put("Data",map1);
                    map.put("MessageName","EUIT.LOTDEFECTIVE");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostMesForwardUtils(map,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotDefectListBean>>(
                        new TypeToken<ResponseBean<LotDefectListBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<LotDefectListBean>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            getDelectInfo(etLotId.getText().toString());
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<LotDefectListBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "LOTDEFECTIVE");
                    }
                });
                break;
            case R.id.flDefectList:
                if (mPositionPop != null){
                    if (mPositionPop.isShowing()) {
                        mPositionPop.dismiss();
                    }
                }else {
                    mPositionPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDefectList.getWidth(), descList, ivDefect, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            tvDefect.setText(s);
                        }
                    });
                }

                FreeApi_PopUtils.setArrowShow(ivDefect);
                mPositionPop.showAsDropDown(flDefectList, 0, 0);
                break;
        }
    }
}

